package com.ykdl.net.response;

import java.lang.reflect.Method;

import com.ykdl.member.base.QLog;

import net.wxxr.dataparse.model.DataParseCode;
import net.wxxr.dataparse.model.DataParseError;
import net.wxxr.http.interfaces.ITag;
import net.wxxr.http.util.ErrorCodeUtil;


/**
 * 通过反射机制对实体bean进行解析与封装
 * 
 * @author yuanwenfei
 * 
 * @param <T>
 */
public class HttpDataResponse<T> extends AndroidHttpResponse<T> {

	@Override
	protected boolean parseData(ITag<T> tag, byte[] data,Object o, Class<T> clazz)
			throws Exception {

		String json = new String(data);
		// 解析数据失败
		QLog.debug("HttpDataResponse json = ", json);
		if (null == json || "".equals(json)) {
			DataParseError error = ErrorCodeUtil
					.getErrorByCode(DataParseCode.DATA_PARSE_FAIL);
			failInUIThread(tag, error);
			return false;
		}

		parseData(json, clazz);
		return true;
	}
	private void parseData(String json, Class<T> clazz) throws Exception {
		// 通过反射的方法执行实体bean中的parse方法
		if(null != clazz){
			Object obj = clazz.newInstance();
			Method m = clazz.getDeclaredMethod("parse", String.class);
			parsedData = m.invoke(obj, json);
		}else{
			parsedData = json;
		}
		
	}

}
